import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/widget/common_app_bar.dart';
import 'package:flutter_shop/widget/water_click.dart';

/// Title:常规输入页面 <br/>
/// Description: <br/>
/// author:Administrator <br/>
/// date: 2020-03-09  <br/>
/// version 1.0 <br/>
class CommonInputPage extends StatelessWidget {
  ///输入类型数组 [0:文本 1:整数 2:小数 ]
  static const List<String> INPUT_TYPE_ARR = ["text", "int", "double"];

  TextEditingController _textEditingController;

  ///标题
  final String title;

  ///内容
  final String content;

  ///hint
  final String hintText;

  ///输入类型
  final String inputType;

  ///最大长度
  final int maxLength;

  ///输入格式
  List<TextInputFormatter> _inputFormatters = [];

  ///键盘类型
  TextInputType keyboardType;

  CommonInputPage({
    this.title = "请输入",
    this.content = "",
    this.inputType,
    this.hintText = "请输入",
    this.maxLength = 200,
  }) {
    _textEditingController = TextEditingController(text: content);
  }

  @override
  Widget build(BuildContext context) {
    _inputFormatters.clear();
    //根据输入类型设置键盘和输入格式
    if (INPUT_TYPE_ARR[0] == inputType) {
      keyboardType = TextInputType.multiline;
    } else if (INPUT_TYPE_ARR[1] == inputType) {
      keyboardType = TextInputType.number;
      _inputFormatters.add(WhitelistingTextInputFormatter.digitsOnly);
    } else if (INPUT_TYPE_ARR[2] == inputType) {
      keyboardType = TextInputType.numberWithOptions(decimal: true);
      _inputFormatters.add(WhitelistingTextInputFormatter(RegExp("[0-9.]")));
    } else {
      keyboardType = TextInputType.multiline;
    }

    return Scaffold(
        appBar: CommonAppBar(title),
        body: Column(
          children: <Widget>[
            Container(
              width: double.infinity,
              padding: EdgeInsets.only(left: 2, top: 2, right: 2, bottom: 5),
              margin: EdgeInsets.only(left: 10, top: 10, right: 10, bottom: 10),
              decoration: BoxDecoration(
                color: CommonColors.white,
                borderRadius: BorderRadius.circular(5),
              ),
              child: TextField(
                controller: _textEditingController,
                keyboardType: keyboardType,
                inputFormatters: _inputFormatters,
                maxLines: 10,
                maxLength: maxLength,
                cursorColor: CommonColors.primary,
                style: TextStyle(fontSize: CommonDimens.font_size_middle, color: CommonColors.second_text),
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: hintText,
                  hintStyle: TextStyle(fontSize: CommonDimens.font_size_middle, color: CommonColors.gray),
                  contentPadding: EdgeInsets.all(2),
                ),
              ),
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: WaterClick(
                    padding: Paddings.common,
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(
                      color: CommonColors.primary,
                      borderRadius: BorderRadius.circular(5),
                    ),
                    onPressed: () {
                      NavigatorUtils.goBackWithParams(context, _textEditingController.text);
                    },
                    child: Text(
                      "确定",
                      style: TextStyle(color: CommonColors.white, fontSize: CommonDimens.font_size_middle),
                    ),
                  ),
                ),
                Expanded(
                  child: WaterClick(
                    padding: Paddings.common,
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(
                      color: CommonColors.red,
                      borderRadius: BorderRadius.circular(5),
                    ),
                    onPressed: () {
                      _textEditingController.clear();
                    },
                    child: Text(
                      "清除",
                      style: TextStyle(color: CommonColors.white, fontSize: CommonDimens.font_size_middle),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ));
  }
}
